package com.huluobo.jetpackkotlindemo.databinding

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.appcompat.app.AppCompatActivity

/**
 * Created by lc on 2022/12/9.
 */
class UserActivity : AppCompatActivity() {
    lateinit var mViewBinding: ActivityUserBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewBinding = ActivityUserBinding.inflate(layoutInflater)
        setContentView(mViewBinding.root)
        val user = User1()
        mViewBinding.user = user
        mViewBinding.userConfirm.setOnClickListener {
//            val user = getUser()
//            mViewBinding.tvUserName.text = user.username
//            mViewBinding.tvUserId.text = user.userId
            //通过双向绑定实现在activity中传递user对象给xml布局
//            mViewBinding.user = user
        }
//        mViewBinding.edUserName.addTextChangedListener(object : TextWatcher {
//            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//            }
//
//            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//
//            }
//
//            override fun afterTextChanged(s: Editable?) {
//            }
//
//        })

//        mViewBinding.user = getUser()
//        mViewBinding.clickHandler = ClickHandler()
//        mViewBinding.clickHandler.user= getUser()
    }

//    fun confirm(view: View) {
//        val user = getUser()
//        mViewBinding.user = user
//    }

    private fun getUser(): User {
        return User(
            getUserName(),
            getUserId(),
            "https://pic3.zhimg.com/v2-20ec1b6c885a660ff3c68238fce5d55b_r.jpg?source=1940ef5c￼",
            1
        )
    }

    private fun getUserId(): String? {
        return mViewBinding.edUserId.text?.toString()
    }

    private fun getUserName(): String? {
        return mViewBinding.edUserName.text?.toString()
    }
}